<?
include_once 'CoreModel.php';
Class CustomerModel extends CoreModel {
	private $table;
	public function __construct() {
		$this -> table = "customer_data";
		parent::__construct();
	}

	public function getCustomerCount($saleID = '', $search) {
		$this -> defaultSearch($search);
		if ($saleID != '') {
			$this -> db -> where("SALES_NO", $saleID);
		}
		$this -> db -> from($this -> table);
		//$this -> db -> count_all_results();

		return $this -> db -> count_all_results();
	}

	public function getCustomer($saleID = '', $search = '', $start = '', $limit = '') {
		$this -> defaultSearch($search);
		$this -> db -> select("*");
		if ($saleID != '') {
			$this -> db -> where("SALES_NO", $saleID);
		}
		$this -> db -> limit($limit, $start);
		$query = $this -> db -> get($this -> table);

		if ($query -> num_rows() > 0) {
			return $query -> result();
		} else {
			return null;
		}
	}

	public function getCustomerArea($saleID = '', $queryString = '', $start = '', $limit = '') {
			
			
		$this->db->select('customer_data.KUNNR,customer_data.NAME,location.Latitude,location.Longitude,GPSResult,GPSResultMessage,customer_data.ADDRESS');
		$this->db->from($this->table);
		$this->db->join('location', 'customer_data.KUNNR = location.KUNNR','left');
		if ($saleID != '') {
			$this -> db -> where("customer_data.SALES_NO", $saleID);
		}
		$this -> db -> where("location.Latitude != ", "0", false);
		$this -> db -> where("location.Longitude != ", "0", false);
		$query = $this->db->get();
		return $query->result();
		
	}

	public function getACustomer($id,$userID="") {
		
		$this->db->select('customer_data.*,location.LATITUDE,location.LONGITUDE,GPSResult,GPSResultMessage');
		$this->db->from($this->table);
		$this->db->where("customer_data.KUNNR",$id);
		if($userID!=""){
			$this->db->where("customer_data.SALES_NO",$userID);
		}
		$this->db->join('location', 'customer_data.KUNNR = location.KUNNR','left');
		
		$query = $this->db->get();
		
		if ($query -> num_rows() > 0) {
			return $query -> row(0);
		}
		return FALSE;
	}
	public function getLocation($custID) {
			
		$this->db->select("*");
		$this->db->where("KUNNR",$custID);
		$query = $this->db->get("location");
		if($query->num_rows() > 0 ){
			$data = $query->row(0);
			unset($data->KUNNR);
		}else{
			return false;
		}
	}
	public function updateLoaction($data) {
			
		$id = $data['KUNNR'];
		$this->db->where("KUNNR",$id);
		$this->db->delete("location");
			
		$this->db->insert("location",$data);
	}
	/*
	public function updateCustomer($data) {
		$id = $data['KUNNR'];
		unset($data['KUNNR']);
		$this -> db -> where("KUNNR", $id);
		$this -> db -> insert($this -> table, $data);
	}
*/
	public function insertCustomerData($result) {
		/* $result is result from sap */

		// default log message
		$data_log['log_function'] = "ZSDRFC_CUSTOMER_DATA";
		$data_log['log_table'] = "CUSTOMER_DATA";
		$data_log['log_record'] = 0;
		$data_log['log_type'] = "INSERT";
		$data_log['log_status'] = "COMPLETED";
		$data_log['log_by_name'] = "Batch Job";
		$data_log['log_page'] = "adminpanel/customer";

		$numRows = 0;
		// start begin trans
		$this -> db -> trans_begin();
		$this -> db -> query('delete from CUSTOMER_DATA');
		$dataInsert = array();
		foreach ($result["CUSTOMER_DATA"] as $cus) {
			$cus["CREDIT_LIMIT"] = $this->util->convertValue($cus["CREDIT_LIMIT"]);
			$cus["CREDIT_USED"] = $this->util->convertValue($cus["CREDIT_USED"]);
			$cus["CREDIT_LEFT"] = $this->util->convertValue($cus["CREDIT_LEFT"]);
			$dataInsert[] = $cus;
		}

		// insert into customer_data;
		$this -> db -> insert_batch($this -> table, $dataInsert);

		// delete sales data;
		$this -> db -> query('delete from SALES_DATA');
		// insert data;
		$this -> db -> insert_batch('SALES_DATA', $result["SALES_DATA"]);

		if ($this -> db -> trans_status() === FALSE) {
			// if error on tranaction
			$this -> db -> trans_rollback();
				
			// insert log  of table customer data
			
			$this->insertLogFailed('CUSTOMER_DATA','adminpanel/customer');
			$this->insertLogFailed('SALES_DATA','adminpanel/customer');
			
			return array("flag" => false, "message" => "error on transfer");
		} else {
			// when complete
			$this -> db -> trans_commit();
			$this->insertLogComplete('CUSTOMER_DATA', count($dataInsert),'adminpanel/customer');
			$this->insertLogComplete('SALES_DATA', count($result["SALES_DATA"]),'adminpanel/customer');			
			return array("flag" => true, "customer_row" => count($dataInsert), "sale_row" => count($result["SALES_DATA"]));
		}
	}

	public function defaultSearch($search) {

		if (empty($search)) {
			return;
		}

		if (isset($search['KUNNR']) && $search['KUNNR'] != "") {
			$this -> db -> like("KUNNR", $search['KUNNR']);
		}
		if (isset($search['NAME']) && $search['NAME'] != "") {
			$this -> db -> like("NAME", $search['NAME']);
		}
		if (isset($search['ADDRESS']) && $search['ADDRESS'] != "") {
			$this -> db -> like("ADDRESS", $search['ADDRESS']);
		}
	}
	/*
	 * SELECT
  KUNNR, (
    3959 * acos (
      cos ( radians(13.693203) )
      * cos( radians( latitude ) )
      * cos( radians( longitude ) - radians(100.493719) )
      + sin ( radians(13.693203) )
      * sin( radians( latitude ) )
    )
  ) AS distance
FROM location
HAVING distance < 40000
ORDER BY distance
LIMIT 0 , 20;
	 * 
	 * */
}
?>